package kt_test.viewmodel

import android.app.Application
import androidx.lifecycle.*
import kotlinx.coroutines.launch
import kt_test.room.database.OxygenDatabase
import kt_test.room.entity.OxygenSampleEntity

class OxygenSampleViewModel(application: Application) : AndroidViewModel(application) {

    private val db = OxygenDatabase.getDatabase(application)

    private val _samples = MutableLiveData<List<OxygenSampleEntity>>()
    val samples: LiveData<List<OxygenSampleEntity>> = _samples

    // 通用查询方法
    fun querySamples(userId: String?, start: String?, end: String?) {
        viewModelScope.launch {
            val result = db.oxygenSampleDao().querySamples(
                if (userId.isNullOrBlank()) null else userId,
                if (start.isNullOrBlank()) null else start,
                if (end.isNullOrBlank()) null else end
            )
            _samples.postValue(result)
        }
    }

    // 默认查询全部
    fun loadAllSamples() {
        querySamples(null, null, null)
    }
}
